<?php

namespace App\Http\Controllers\Archive;

use App\Http\Controllers\Controller;
use App\Repositories\Contracts\PatternRepositoryInterface;
use Illuminate\Http\Request;

class PatternController extends Controller {

  /**
   * @var \App\Repositories\CategoryRepository
   */
  private $patternRepository;

  public function __construct( PatternRepositoryInterface $patternRepository ) {
    $this->patternRepository = $patternRepository;
  }

  public function lists( $id = null, Request $request ) {
    $result = $this->patternRepository->paginate();
    return response()->json( [ 'success' => true, 'data' => $result ] );
  }

  public function save( Request $request ) {
    $id   = intval( $request->input( "id", 0 ) );
    $data = $request->only( 'name', 'category_template', 'list_template' , 'show_template');
    if ( $id > 0 ) {
      $data = $this->patternRepository->updateRich( $data, $id );
    } else {
      $data = $this->patternRepository->create( $data );
    }

    return $this->result( $data != null, $data );

  }



  public function remove( $id = null, Request $request ) {
    $id = intval( $id );
    if ( $id < 0 ) {
      return $this->result( false );
    }
    $result = $this->patternRepository->delete( $id );

    return $this->result( $result > 0 );

  }
}
